﻿import { ttwin } from "./ttwin.js"

window.onload = async () => {
    var br = document.createElement("br");
    document.body.appendChild(br);


    let result = await ttwin.Init();
    if(result)
    {
        addLine( "ext localexturl=" + ttwin.GetUrl());


        testLocalExt();
    }
    else
    {
        addLine("当前环境不具有 本地文件访问扩展，请检查");
    }
}





async function testLocalExt() {
    var br = document.createElement("br");
    document.body.appendChild(br);
    var info = await ttwin.Info();
    addLine("Info=" + info);


    var d = await ttwin.SetDebug(false);

    addLine("SetDebug=" + d);

    var br = document.createElement("br");
    document.body.appendChild(br);



    var btn = document.createElement("button");
    document.body.appendChild(btn);
    btn.textContent = "test dir";
    btn.onclick = test_dir;

    var br = document.createElement("br");
    document.body.appendChild(br);
    var btn = document.createElement("button");
    document.body.appendChild(btn);
    btn.textContent = "test file";
    btn.onclick = test_file;


    var br = document.createElement("br");
    document.body.appendChild(br);
    var btn = document.createElement("button");
    document.body.appendChild(btn);
    btn.textContent = "test Quit";
    btn.onclick = test_quit;
}
async function test_dir() {

    var roots = await ttwin.Path_GetRoots();
    addLine("==getroots logic=" + JSON.stringify(roots.logic));
    addLine("==getroots HtmlPath=" + JSON.stringify(roots.special.HtmlPath));
    addLine("==getroots WorkingPath=" + JSON.stringify(roots.special.WorkingPath));
    addLine("==getroots MyDocuments=" + JSON.stringify(roots.special.MyDocuments));


    var oinfo = await ttwin.Path_Info(roots.special.WorkingPath);
    addLine("==Path_Info exists=" + oinfo.exists);
    addLine("==Path_Info name=" + oinfo.Name);

    addLine("==Path_Info LastWriteTime=" + oinfo.LastWriteTime);


    var olist = await ttwin.Path_List(roots.special.MyDocuments, false, "*.json");
    addLine("==Path_List dirs=" + olist.dirs.toString());
    addLine("==Path_List files=" + olist.files.toString());

    var info = await ttwin.Path_Create("D:\\git\\__001");

    addLine("==Path_Create " + info);

    var info = await ttwin.Path_Create("D:\\git\\__001");

    addLine("==Path_Create " + info);

    var info = await ttwin.Path_Delete("D:\\git\\__002");

    addLine("==Path_Delete " + info);

    var info = await ttwin.Path_Create("D:\\git\\__003");

    addLine("==Path_Create " + info);
}
async function test_quit() {
    let r = await ttwin.Quit();

    addLine("==Quit==" + r);
}
async function test_file() {

    var roots = await ttwin.Path_GetRoots();

    let list = await ttwin.Path_List(roots.special.MyDocuments, false, "*.json");


    var file = list.files[0];
    addLine("==find file=" + file);

    var oinfo = await ttwin.File_Info(file);

    addLine("==file_info file=" + oinfo);

    var oinfo = await ttwin.File_Info("d:\\ttt.jpg");
    addLine("==file_info ttt file=" + oinfo);

    //test delete
    var r = await await ttwin.File_Delete("d:\\ttt.jpg");
    addLine("==file_delete ttt file=" + r);
    var oinfo = await ttwin.File_Info("d:\\ttt.jpg");
    addLine("==file_info ttt file=" + oinfo);
    //test copy
    var r = await ttwin.File_Copy(file, "d:\\ttt.jpg");

    addLine("==file_copy=" + r);

    var oinfo = await ttwin.File_Info("d:\\ttt.jpg");
    addLine("==file_info ttt file=" + oinfo);
    //test read
    var binfo = await ttwin.File_ReadBinary(file);

    var f = binfo.data;
    addLine("load file len=" + binfo);
    //test write

    if (f != undefined) {
        var r = await ttwin.File_WriteBinary("d:\\ttt2.jpg", f);
        addLine("==file_write ttt2 file=" + r);
    }
    //const headers = new Headers();
    //headers.append("Content-Type", "text/plain; charset=utf-8"); // 指定编码为 utf-8
    var r = await ttwin.File_WriteText("d:\\ttt3.info", "very good.地球上的啥子");
    addLine("==file_write ttt3 file=" + r);


    let b = new Blob(["hasdfd地球上"]);

    var r = await ttwin.File_WriteBlob("d:\\ttt3.info", b);
    addLine("==file_write ttt4 file=" + r);

    //test read
    var text = await ttwin.File_ReadText("d:\\ttt4.info");
    addLine("code=" + "load text=" + text);
    //test read error
    var text = await ttwin.File_ReadText("d:\\ttt44.info");
    addLine("code=" + "load text=" + text);

}
function addLine(txt: string) {
    var br = document.createElement("br");
    document.body.appendChild(br);
    var span = document.createElement("span");
    span.textContent = txt;
    document.body.appendChild(span);
}